

alter trigger [dbo].[tfd_wagefeebase_au] on [dbo].[tfd_wagefeebase]
after update 
as
  declare @iyear integer;
  declare @imonth integer;
  declare @idothreefeemonth integer;
  declare @ihavedothreefee integer;
  declare @idodividemonth integer;
  declare @ihavedodivide integer;
  declare @nfreeid integer;
  declare @ndebitsubid integer;
  declare @ncreditsubid integer;
  declare @nddepartmentid integer;
  declare @ncdepartmentid integer;
  declare @ndempid integer;
  declare @ncempid integer;
  declare @ndprojectid integer;
  declare @ncprojectid integer;
  declare @ndproductid integer;
  declare @ncproductid integer;
  declare @ndtraderid integer;
  declare @nctraderid integer;
  declare @nemptypeid integer;
  declare @ndepartmentid integer;
  declare @nfeetypeid integer;
begin
  set nocount on
  declare tfd_wagefeebase_cr cursor local forward_only static optimistic 
  for select freeid,
	 debitsubid, creditsubid,
	 ddeptid, cdeptid,
	 dempid, cempid,
	 dprojectid, cprojectid,
	 dproductid, cproductid,
	 dtraderid, ctraderid,
	 emptypeid, deptid,
	 feetypeid
    from inserted
  open tfd_wagefeebase_cr
  fetch next from tfd_wagefeebase_cr into @nfreeid,
    @ndebitsubid, @ncreditsubid,
	@nddepartmentid, @ncdepartmentid,
	@ndempid, @ncempid,
	@ndprojectid, @ncprojectid,
	@ndproductid, @ncproductid,
	@ndtraderid, @nctraderid,
	@nemptypeid, @ndepartmentid,
	@nfeetypeid
  while @@fetch_status = 0
  begin
    if (@nfreeid<>99) 
    begin
      if ((@nfreeid<4)) 
      begin
        update tfb_wagefeedtl set debitsubid=@ndebitsubid,creditsubid=@ncreditsubid,
                                ddeptid=@nddepartmentid,cdeptid=@ncdepartmentid,
                                dempid=@ndempid,cempid=@ncempid,
                                dprojectid=@ndprojectid,cprojectid=@ncprojectid,
                                dproductid=@ndproductid,cproductid=@ncproductid,
                                dtraderid=@ndtraderid,ctraderid=@nctraderid
          where freeid=@nfreeid and emptypeid=@nemptypeid and deptid=@ndepartmentid 
            and bid in (select bid from tfb_wagefee where bstate=0);
      end
    end
    else
    begin
      update tfb_wagedividedtl set debitsubid=@ndebitsubid,creditsubid=@ncreditsubid,
                             ddeptid=@nddepartmentid,cdeptid=@ncdepartmentid,
                             dempid=@ndempid,cempid=@ncempid,
                             dprojectid=@ndprojectid,cprojectid=@ncprojectid,
                             dproductid=@ndproductid,cproductid=@ncproductid,
                             dtraderid=@ndtraderid,ctraderid=@nctraderid
        where emptypeid=@nemptypeid and deptid=@ndepartmentid
          and feetypeid=@nfeetypeid and bid in (select bid from tfb_wagedivide where bstate=0);
    end
    fetch next from tfd_wagefeebase_cr into @nfreeid,
    @ndebitsubid, @ncreditsubid,
	@nddepartmentid, @ncdepartmentid,
	@ndempid, @ncempid,
	@ndprojectid, @ncprojectid,
	@ndproductid, @ncproductid,
	@ndtraderid, @nctraderid,
	@nemptypeid, @ndepartmentid,
	@nfeetypeid    
  end
end



